/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.datamodel;

import br.jm.financeiro.entity.CepEndereco;
import br.jm.financeiro.sistema.eao.EnderecoEao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author joao
 */
public class LazyLogradouro extends LazyDataModel<CepEndereco> {
    private static final long serialVersionUID = -9048109356445687665L;

    private List<CepEndereco> datasource;
    private final EnderecoEao enderecoJpa;
    private String cFiltro;
    private String cParametroSQL;
    private boolean lSorter;
    int dataSize;
    private FacesContext ctx;
    private HttpSession session;
    private Map<String, Object> pesquisas = new HashMap<>();

    public LazyLogradouro(EnderecoEao enderecoJpa) {
        this.enderecoJpa = enderecoJpa;
        this.dataSize = enderecoJpa.getCount("CepEndereco");
    }

    @Override
    public CepEndereco getRowData(String rowKey) {
        for (CepEndereco endereco : datasource) {
            if (endereco.getDscLogradouro().equals(rowKey)) {
                return endereco;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(CepEndereco endereco) {
        return endereco.getDscLogradouro();
    }

    @Override
    public void setRowIndex(int rowIndex) {
        /*
         * The following is in ancestor (LazyDataModel):
         * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
         */
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }

    @Override
    public List<CepEndereco> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        ctx = FacesContext.getCurrentInstance();
        session = (HttpSession) ctx.getExternalContext().getSession(false);
        pesquisas =  (Map<String, Object>) session.getAttribute("pesquisa");

        if (pesquisas.size() > 0) {
            filters = pesquisas;
        }
        List<CepEndereco> data = new ArrayList<>();
        try {
            //filter
            lSorter = SortOrder.ASCENDING.equals(sortOrder);

            data = enderecoJpa.getNative("CepEndereco", first, pageSize, sortField, lSorter, filters, cParametroSQL, cFiltro);
        } catch (Exception ex) {
            //Logger.getLogger(LazyLogradouro.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setRowCount(dataSize);
        if (filters.isEmpty()) {
            if (data.isEmpty()) {
                this.setRowCount(0);
            } else {
                this.setRowCount(this.dataSize);
            }
        }

        //paginate
        return data;
    }
}